package daf



import org.junit.*
import grails.test.mixin.*


@TestFor(AppController)
@Mock(App)
class AppControllerTests {


    @Test
    void testIndex() {
        controller.index()
        assert "/app/list" == response.redirectedUrl
    }

    @Test
    void testList() {

        def model = controller.list()

        assert model.appInstanceList.size() == 0
        assert model.appInstanceTotal == 0

    }

    @Test
    void testCreate() {
       def model = controller.create()

       assert model.appInstance != null


    }

    @Test
    void testSave() {
        controller.save()

        assert model.appInstance != null
        assert view == '/app/create'

        // TODO: Populate valid properties

        controller.save()

        assert response.redirectedUrl == '/app/show/1'
        assert controller.flash.message != null
        assert App.count() == 1
    }


    @Test
    void testShow() {
        controller.show()

        assert flash.message != null
        assert response.redirectedUrl == '/app/list'


        def app = new App()

        // TODO: populate domain properties

        assert app.save() != null

        params.id = app.id

        def model = controller.show()

        assert model.appInstance == app
    }

    @Test
    void testEdit() {
        controller.edit()

        assert flash.message != null
        assert response.redirectedUrl == '/app/list'


        def app = new App()

        // TODO: populate valid domain properties

        assert app.save() != null

        params.id = app.id

        def model = controller.edit()

        assert model.appInstance == app
    }

    @Test
    void testUpdate() {

        controller.update()

        assert flash.message != null
        assert response.redirectedUrl == '/app/list'

        response.reset()


        def app = new App()

        // TODO: populate valid domain properties

        assert app.save() != null

        // test invalid parameters in update
        params.id = app.id

        controller.update()

        assert view == "/app/edit"
        assert model.appInstance != null

        app.clearErrors()

        // TODO: populate valid domain form parameter
        controller.update()

        assert response.redirectedUrl == "/app/show/$app.id"
        assert flash.message != null
    }

    @Test
    void testDelete() {
        controller.delete()

        assert flash.message != null
        assert response.redirectedUrl == '/app/list'

        response.reset()

        def app = new App()

        // TODO: populate valid domain properties
        assert app.save() != null
        assert App.count() == 1

        params.id = app.id

        controller.delete()

        assert App.count() == 0
        assert App.get(app.id) == null
        assert response.redirectedUrl == '/app/list'


    }


}